package com.morgan.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CTMapSort1 {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        
        Map map = new HashMap();
        map.put("aaaa", "10");
        map.put("aaaa1", "30");
        map.put("aaaa2", "20");

        List list = new ArrayList();

        Map.Entry[] entrys = getSortedHashtableByKey(map);
        if (entrys != null && entrys.length > 0) {
            for (int i = 0; i < entrys.length; i++) {
                Map.Entry entry = entrys[i];
                if (entry != null) {
                    list.add(entry.getValue());
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    public static Map.Entry[] getSortedHashtableByKey(Map h) {

        Set set = h.entrySet();

        Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]);
        Arrays.sort(entries, new Comparator() {
            public int compare(Object arg0, Object arg1) {
                Object key1 = ((Map.Entry) arg0).getKey();
                Object key2 = ((Map.Entry) arg1).getKey();
                return ((Comparable) key1).compareTo(key2);
            }

        });
        return entries;
    }

}
